package com.jx.lcs.product.web;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tianyu.jty.common.persistence.Page;
import com.tianyu.jty.common.persistence.PropertyFilter;
import com.tianyu.jty.common.web.BaseController;
import com.jx.lcs.product.entity.ProductType;
import com.jx.lcs.product.service.ProductTypeService;

/**
 * 商品类型类型controller
 * @author ty
 * @date 2015年1月22日
 */
@Controller
@RequestMapping("product/productType")
public class ProductTypeController extends BaseController{

	private static final Logger logger = LoggerFactory.getLogger(ProductController.class);
	@Autowired
	private ProductTypeService productTypeService;

	/**
	 * 默认页面
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String list() {
		return "product/productTypeList";
	}
	
	/**
	 * 获取商品类型类型json
	 */
	@RequestMapping(value="json",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> productTypeList(HttpServletRequest request) {
//		setOrderBy("ordernum");
//		setOrder("desc");
		Page<ProductType> page = getPage(request);
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(request);
		page = productTypeService.search(page, filters);
		return getEasyUIData(page);
	}
	
	/**
	 * 添加商品类型跳转
	 * 
	 * @param model
	 */
	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String createForm(Model model) {
		model.addAttribute("productType",new ProductType());
		model.addAttribute("action", "create");
		return "product/productTypeForm";
	}

	/**
	 * 添加商品类型
	 * 
	 * @param productType
	 * @param model
	 */
	@RequestMapping(value = "create", method = RequestMethod.POST)
	@ResponseBody
	public String create(@Valid ProductType productType, Model model) {
		try{
			productType.setId(UUID.randomUUID().toString());
			productTypeService.save(productType);
		}catch(Exception e){
			logger.error("--添加产品出现异常！！", e);
		}
		return "success";
	}

	/**
	 * 修改商品类型跳转
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String updateForm(@PathVariable("id") String id, Model model) {
		model.addAttribute("productType", productTypeService.get(id));
		model.addAttribute("action", "update");
		return "product/productTypeForm";
	}

	/**
	 * 修改商品类型
	 * 
	 * @param productType
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public String update(@Valid @ModelAttribute @RequestBody ProductType productType,Model model) {
		productTypeService.update(productType);
		return "success";
	}

	/**
	 * 删除商品类型
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "delete/{id}")
	@ResponseBody
	public String delete(@PathVariable("id") String id) {
		productTypeService.delete(id);
		return "success";
	}
	
	@ModelAttribute
	public void getProductType(@RequestParam(value = "id", defaultValue = "-1") String id,Model model) {
		if (id.equals("")) {
			model.addAttribute("productType", productTypeService.get(id));
		}
	}
	
	/**
	 * 获取所有商品类型类型list
	 */
	@RequestMapping(value="listAll",method = RequestMethod.GET)
	@ResponseBody
	public List<ProductType> findProductTypeList() {
		return productTypeService.getEntityDao().findAll("ordernum", true);
	}
}
